package com.zhan.signalling.store.mongo;

import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.zhan.signalling.entity.ChatRecord;
import com.zhan.signalling.entity.WhiteBoardRecord;
import com.zhan.signalling.server.SocketServer;
import com.zhan.signalling.store.StoreMessageStore;

public class MongoStoreMessageStore extends StoreMessageStore {
	
	protected MongoClient mongoClient;
	
	public final static String name = "mongodb";
	
	private MongoStoreMessageStore(MongoClient mongoClient) {
		this.mongoClient = mongoClient;
	}

	public static StoreMessageStore getHandler(Properties prop) {
			
		final String host = prop.getProperty(SocketServer.STORE_MESSAGE_PROP+".mongodb.host");
		final Integer port = Integer.valueOf(prop.getProperty(SocketServer.STORE_MESSAGE_PROP+".mongodb.port"));
		final String username = prop.getProperty(SocketServer.STORE_MESSAGE_PROP+".mongodb.username");
		final String password = prop.getProperty(SocketServer.STORE_MESSAGE_PROP+".mongodb.password");
		final String database = prop.getProperty(SocketServer.STORE_MESSAGE_PROP+".mongodb.database");
		final Integer connectionsPerHost = Integer.valueOf(prop.getProperty(SocketServer.STORE_MESSAGE_PROP+".mongodb.connections-per-host"));
		final Integer minConnectionsPerHost = Integer.valueOf(prop.getProperty(SocketServer.STORE_MESSAGE_PROP+".mongodb.min-connections-per-host"));
		
	    MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.connectionsPerHost(connectionsPerHost);
        builder.minConnectionsPerHost(minConnectionsPerHost);
        MongoClientOptions mongoClientOptions = builder.build();

        ServerAddress serverAddress = new ServerAddress(host, port);

        MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(
        		username,
        		database,
        		password.toCharArray());
        MongoClient mongoClient = new MongoClient(serverAddress, mongoCredential, mongoClientOptions);
		
		return new MongoStoreMessageStore(mongoClient);
	}

	@Override
	public long saveChat(ChatRecord message) {
		// TODO Auto-generated method stub
		return super.saveChat(message);
	}

	@Override
	public long saveWhiteBoard(WhiteBoardRecord message) {
		// TODO Auto-generated method stub
		return super.saveWhiteBoard(message);
	}

	@Override
	public List<Map<String,String>> getHistoryMessage(String platform, String groupId, Long seqId) {
		// TODO Auto-generated method stub
		return super.getHistoryMessage(platform, groupId, seqId);
	}
	
	
}
